package com.onairm.baselibrary.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.onairm.baselibrary.Init;

/**
 * Created by bqy on 2018/3/26.
 */

public class ImageManager {

    private static String qNiuHost;
    private static int placeHolder;
    private static int errorHolder;
    private static Context context;

    public static String getUrl(String str) {
//        if (SharePreferences.isSaveFlow() && !Tools.isWifi()) { //非wifi无图模式
//            return null;
//        }
        if (str != null && !str.equals("")) {
            if (str.contains("http")) {
                return str;
            } else {
                return "http://" + getqNiuHost() + "/" + str;
            }
        } else {
            return null;
        }
    }

    public static String getImageUrl(String str, String qiniuParams) {
        if (str != null && !str.equals("")) {
            if (str.contains("http")) {
                if (str.contains(getqNiuHost())) {
                    if (str.contains("?")) {
                        if (!str.endsWith("?")) {
                            if (!TextUtils.isEmpty(qiniuParams)) {
                                str = str + "|" + qiniuParams;
                            }
                        }
                    } else {
                        str = str + "?" + qiniuParams;
                    }
                }
            } else {
                if (str.contains("?")) {
                    if (!str.endsWith("?")) {
                        if (!TextUtils.isEmpty(qiniuParams)) {
                            str = str + "|" + qiniuParams;
                        }
                    }
                } else {
                    str = str + "?" + qiniuParams;
                }
                str = "http://" + getqNiuHost() + "/" + str;
            }
            return str;
        } else {
            return null;
        }
    }

    public static String getqNiuHost() {
        if (TextUtils.isEmpty(qNiuHost)) {
            return SharePrefer.getQiniuHost();
        }
        return qNiuHost;
    }

    public static void setqNiuHost(String qNiuHost) {
        ImageManager.qNiuHost = qNiuHost;
    }

    public static int getPlaceHolder() {
        return placeHolder;
    }

    public static void setPlaceHolder(int placeHolder) {
        ImageManager.placeHolder = placeHolder;
    }

    public static int getErrorHolder() {
        return errorHolder;
    }

    public static void setErrorHolder(int errorHolder) {
        ImageManager.errorHolder = errorHolder;
    }

    public static void setContext(Context context) {
        ImageManager.context = context;
    }

    public static void showImage(String url, ImageView imageView) {
        Glide.with(Init.context)
                .load(getUrl(url))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void showImage(String url, ImageView imageView, String params) {
        Glide.with(Init.context)
                .load(getImageUrl(url, params))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void showRoundImage(String url, ImageView imageView, int round) {
        Glide.with(Init.context)
                .load(getUrl(url))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(context, round))
                .into(imageView);
    }

    public static void showRoundImage(String url, ImageView imageView, String params, int round) {
        Glide.with(Init.context)
                .load(getImageUrl(url, params))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(context, round))
                .into(imageView);
    }

    public static void showImage(Context context, String url, ImageView imageView) {
        Glide.with(context)
                .load(getUrl(url))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void showImage(Context context, String url, ImageView imageView, String params) {
        Glide.with(context)
                .load(getImageUrl(url, params))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public static void showRoundImage(Context context, String url, ImageView imageView, int round) {
        Glide.with(context)
                .load(getUrl(url))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(context, round))
                .into(imageView);
    }

    public static void showRoundImage(Context context, String url, ImageView imageView, String params, int round) {
        Glide.with(context)
                .load(getImageUrl(url, params))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(context, round))
                .into(imageView);
    }

    public static void showRoundImage(Context context, String url, ImageView imageView,
                                      int placeHolder, int errorHolder, String params, int round) {
        Glide.with(context)
                .load(getImageUrl(url, params))
                .placeholder(placeHolder)
                .error(errorHolder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .transform(new GlideRoundTransform(context, round))
                .into(imageView);
    }

    public static String getQiNiuParams(int w, int h) {
        return "imageView2/0/w/" + w + "/h/" + h + "/q/10";
    }
}
